package com.snailhd.listener;

import com.snailhd.cloud.redis.service.RedisService;
import com.snailhd.redis.RedisKeys;
import com.snailhd.entity.StockDay;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hd
 * @description
 * @date 2019-11-23 16:08
 */
@Component
public class StockDaySaveRedisListener implements Listener<List<StockDay>> {

    @Reference
    private RedisService redisService;

    @Override
    public void monitor(List<StockDay> stockDays) {
        if(null!=stockDays){
            Map<String,String> hash =
                    stockDays.stream().collect(Collectors.toMap(StockDay::getCode,StockDay::toJSONString,(k1, k2)->k2));
            redisService.hmset(RedisKeys.STOCK_DAY,hash);
        }
    }
}
